#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long

void solve()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    s = ' ' + s;
    vector<int> ans(n + 10);
    int res = 0x3f3f3f3f;
    for(int i = 1; i <= n; i ++)
    {
        if(s[i] == 'a') ans[i] = -1;
        else ans[i] = 1;
    }
    for(int i = 1;  i <= n; i ++) ans[i] = ans[i - 1] + ans[i];
    if (ans[n] == 0) {
		std::cout << 0 << "\n";
		return;
	}
    map<int,int> mp;
    mp[0] = 0;
    for(int i = 1; i <= n; i ++)
    {
        if(mp.count(ans[i] - ans[n]))
        {
            res = min(res,i - mp[ans[i] - ans[n]]);
        }
        mp[ans[i]] = i;
    }
    if (res == n) {
		res = -1;
	}
    cout << res << endl;
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}